import {Component, EventEmitter, Input, Output} from '@angular/core';
import {Util} from "@providers/util";

/**
 * Generated class for the DatePickerComponent component.
 *
 * See https://angular.io/api/core/Component for more info on Angular
 * Components.
 */
@Component({
  selector: 'date-picker',
  templateUrl: 'date-picker.html'
})
export class DatePickerComponent {

  today: any = "";
  dates: any = [];
  date_type: any = 1;
  state: any = {
    GROUP: true,
    date_type: true,
    week_type: true
  };
  @Output('change') change = new EventEmitter();

  @Input("ngSwitch") ngSwitch;
  @Input("segment") segment;

  WEEK: any = [];
  CURRENT: any = {};

  constructor() {
  }

  ngAfterContentInit() {
      this.today = Util.getDate(0);
      this.changeDateType(1);
    this.CURRENT.year = Number(Util.getYear(0));
    this.CURRENT.month = Number(Util.getMonth(0, "MM"));

    this.initCanlender();
  }

  initCanlender() {
    let thisDate, startWeek;
    let dateTime = this.CURRENT;
    thisDate = new Date(dateTime.year, dateTime.month - 1, 1);
    startWeek = thisDate.getDay();
    this.WEEK = [];
    for (let i = 0; i < 6; i++) {
      let week = [];
      if (i == 0) {
        thisDate = new Date(thisDate.getTime() - (startWeek + 1) * 24 * 60 * 60 * 1000);
        for (let m = 0; m < 7; m++) {
          thisDate = new Date(thisDate.getTime() + 24 * 60 * 60 * 1000);
          week.push({
            "key": Util.dateFormat(thisDate, "YYYY-MM-DD"),
            "value": Number(Util.dateFormat(thisDate, "DD"))
          })
        }
        this.WEEK.push(week);
      } else {
        for (let m = 0; m < 7; m++) {
          thisDate = new Date(thisDate.getTime() + 24 * 60 * 60 * 1000);
          week.push({
            "key": Util.dateFormat(thisDate, "YYYY-MM-DD"),
            "value": Number(Util.dateFormat(thisDate, "DD"))
          });
        }
        this.WEEK.push(week);
      }
    }
  }

  setCurrentYear(num) {
    this.CURRENT.year += num;
    this.initCanlender();
  }

  setCurrentMonth(num) {
    this.CURRENT.year = Number(Util.getYear(new Date(this.CURRENT.year, this.CURRENT.month + num - 1, 1)));
    this.CURRENT.month = Number(Util.getMonth(new Date(this.CURRENT.year, this.CURRENT.month + num - 1, 1), "MM"));
    this.initCanlender();
  }

  changeStateDateType() {
    this.state.date_type = !this.state.date_type;
    this.state.week_type = true;
  }

  mode: string = "radio";

  changeDateType(date_type) {
    this.mode = "radio";
    this.date_type = date_type;

    if (date_type == 0) {
      this.mode = "multi";
      this.state.week_type = false;
      this.dates = [];
    } else if (date_type == 1) {
      let date1 = Util.stringToDate(this.today).getTime() - 24 * 60 * 60 * 1000;
      let date2 = Util.stringToDate(this.today).getTime() + 24 * 60 * 60 * 1000;
      this.dates = [Util.dateToString(new Date(date1)), this.today, Util.dateToString(new Date(date2))];
    } else if (date_type == 2) {
      this.mode = "week";
      //this.state.week_type = false;
      this.dates = [Util.getWeekStart(0), Util.getWeekEnd(0)];
    } else if (date_type == 3) {
      this.dates = [Util.getMonth(-1), Util.getMonth(0), Util.getMonth(1)];
    } else if (date_type == 4) {
      this.dates = [Util.getYear(-1), Util.getYear(0), Util.getYear(1)];
    }
    //this.query();
    this.swipeDate(0);
  }

  selectDate(ITEM) {
    if (this.mode == "week") {
      this.dates = [Util.getWeekStart(Util.stringToDate(ITEM.key)), Util.getWeekEnd(Util.stringToDate(ITEM.key))];
      this.state.week_type = true;
    } else if (this.mode == "radio") {
      let date1 = Util.stringToDate(ITEM.key).getTime() - 24 * 60 * 60 * 1000;
      let date2 = Util.stringToDate(ITEM.key).getTime() + 24 * 60 * 60 * 1000;
      this.dates = [Util.dateToString(new Date(date1)), ITEM.key, Util.dateToString(new Date(date2))];
    } else if (this.mode == "multi") {
      if (this.dates.length == 2) {
        this.dates = [];
      }
      this.dates.push(Util.getDate(Util.stringToDate(ITEM.key)));
      if (this.dates.length == 2) {
        this.state.week_type = true;
      }
      if (this.dates[0] > this.dates[1]) {
        this.dates.sort()
      }
    }
    this.emit();
  }

  emit() {
    if (this.date_type == 0) {
      if (this.dates.length == 2) {
        this.change.emit({
          type: this.date_type,
          date: this.dates
        });
      }
    } else if (this.date_type == 2) {
      this.change.emit({
        type: this.date_type,
        date: this.dates
      });
    } else {
      this.change.emit({
        type: this.date_type,
        date: this.dates[1]
      });
    }
  }

  chooseDate() {
    this.state.week_type = !this.state.week_type;
  }

  swipeDate(d) {
    if (d == -1) {
      //this.dates[0];
      if (this.date_type != 0 && this.date_type != 2) {
        this.dates[2] = this.dates[1];
        this.dates[1] = this.dates[0];
      }
      if (this.date_type == 1) {
        let date1 = Util.stringToDate(this.dates[0]).getTime() - 24 * 60 * 60 * 1000;
        this.dates[0] = Util.dateToString(new Date(date1));
      } else if (this.date_type == 2) {
      } else if (this.date_type == 3) {
        let date = Util.stringToDate(this.dates[0] + "-01");
        date.setMonth(date.getMonth() - 1);
        this.dates[0] = Util.getMonth(date);
      } else if (this.date_type == 4) {
        let date = Util.stringToDate(this.dates[0] + "-01-01");
        date.setFullYear(date.getFullYear() - 1);
        this.dates[0] = Util.getYear(date);
      }
    } else if (d == 1) {
      if (this.date_type != 0 && this.date_type != 2) {
        this.dates[0] = this.dates[1];
        this.dates[1] = this.dates[2];
      }
      if (this.date_type == 1) {
        let date1 = Util.stringToDate(this.dates[2]).getTime() + 24 * 60 * 60 * 1000;
        this.dates[2] = Util.dateToString(new Date(date1));
      } else if (this.date_type == 2) {
      } else if (this.date_type == 3) {
        let date = Util.stringToDate(this.dates[2] + "-01");
        date.setMonth(date.getMonth() + 1);
        this.dates[2] = Util.getMonth(date);
      } else if (this.date_type == 4) {
        let date = Util.stringToDate(this.dates[2] + "-01-01");
        date.setFullYear(date.getFullYear() + 1);
        this.dates[2] = Util.getYear(date);
      }
    }
    this.state.date_type = true;
    this.emit();
  }

}
